/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
* @author truongtud
*/
package ServiceOfRestful;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.ws.rs.Consumes;

import javax.ws.rs.GET;
import static javax.ws.rs.HttpMethod.POST;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.json.simple.*;

/**
 *
 * @author truongtud
 */
@Path("/shoppingcart")
public class ShoppingCartRestful {

    HashMap<String, Integer> cart = null;

    public ShoppingCartRestful() {
    }

    public ArrayList<Product> getProducts() {
        return ShoppingCartStartup.products;
    }
    // @Override

    @GET
    @Path("/addUser/{UserID}")
    @Produces(MediaType.TEXT_PLAIN)
    public String addUser(@PathParam("UserID") int UserID) {
        String check = "";
        cart = new HashMap<String, Integer>();
        if (cart != null) {
            System.out.println("Cart  duuoc tao ra ");
        }
        ShoppingCartStartup.clients.put(new Integer(UserID), cart);
        System.out.println("tao ra " + ShoppingCartStartup.clients.size());
        return new StringBuilder().append(UserID).append(check).toString();
    }

    @GET
    @Path("/getAllProduct")
    @Produces(MediaType.TEXT_PLAIN)

    public String getAllProductsText() {
        StringBuffer str = new StringBuffer();
        for (Object obj : ShoppingCartStartup.products.toArray()) {
            Product pro = (Product) obj;
            str.append(pro.getName() + "-");
            str.append(pro.getPrice() + "-");
            str.append(pro.getAmount());
            str.append(":");
        }
        return str.substring(0, str.length() - 1).toString();
    }
    // @Override

    @GET
    @Path("/addProduct/{UserID}/{ProductID}")
    @Produces(MediaType.TEXT_PLAIN)
    public String addProduct(@PathParam("UserID") int UserID, @PathParam("ProductID") String ProductID) {
        System.out.println("chieu dai cua list" + ShoppingCartStartup.products.size());
        cart = ShoppingCartStartup.clients.get(UserID);
        //addItem
        if (cart.containsKey(ProductID)) {
            int newQuantity = ((Integer) cart.get(ProductID)).intValue() + 1;
            cart.remove(ProductID);
            cart.put(ProductID, newQuantity); //update cart
        } else {
            Integer in = new Integer(1);
            cart.put(ProductID, in);
        }
        System.out.println("Chieu dai cua cart" + cart.size());
        ShoppingCartStartup.clients.remove(UserID);
        ShoppingCartStartup.clients.put(UserID, cart);//undapte list of user
        StringBuilder str = new StringBuilder();
        str.append(UserID);
        str.append(ProductID);
        return str.toString();

    }

    //@Override
    @GET
    @Path("/removeProduct/{UserID}/{ProductID}")
    @Produces(MediaType.TEXT_PLAIN)
    public void removeProduct(int UserID, String ProductID) {
        cart = ShoppingCartStartup.clients.get(UserID);
        int newQuantity = ((Integer) (cart.get(ProductID))).intValue() - 1;
        cart.remove(ProductID);
        cart.put(ProductID, newQuantity);//update cart
        ShoppingCartStartup.clients.remove(UserID);
        ShoppingCartStartup.clients.put(UserID, cart);//undapte list of user
        // return this.getAllProducts();
    }

    @GET
    @Path("/checkOut/{UserID}")
    @Produces(MediaType.TEXT_PLAIN)
    public synchronized String checkOutText(@PathParam("UserID") int UserID) {
        cart = ShoppingCartStartup.clients.get(UserID);
        Iterator productIDs = cart.keySet().iterator();      
        while (productIDs.hasNext()) {
            String isAvailable="false";
            String productId = (String) productIDs.next();
            int quantity = (Integer) cart.get(productId).intValue();
            for (Object obj : ShoppingCartStartup.products.toArray()) {
                Product product = (Product) obj;
                if (product.getName().equals(productId)) {
                    int newAmount = product.getAmount() - quantity;
                    if (newAmount == 0) {
                        ShoppingCartStartup.products.remove(product);
                        isAvailable="true";
                    } 
                   else if(newAmount>0){
                        product.setAmount(newAmount);
                        isAvailable="true"; 
                    }
                    else
                       break;
                   
                    
                } 
            }
            if(isAvailable=="false"){
                 ShoppingCartStartup.clients.put(UserID, new HashMap<String,Integer>());
                return "false";
            }
        }
        ShoppingCartStartup.clients.put(UserID, new HashMap<String,Integer>());
        return "true";
    }

}
